home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / emacs_src_18_58.lha / emacs-18.58 / src / callproc.c < prev    next >
C/C++ Source or Header  |  1992-09-22  |  13KB  |  509 lines

  1. /* Synchronous subprocess invocation for GNU Emacs.
  2.    Copyright (C) 1985, 1986, 1987, 1988, 1990 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU Emacs.
  5.  
  6. GNU Emacs is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 1, or (at your option)
  9. any later version.
  10.  
  11. GNU Emacs is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU Emacs; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. #include <signal.h>
  22.  
  23. #include "config.h"
  24.  
  25. #include <sys/types.h>
  26. #define PRIO_PROCESS 0
  27. #include <sys/file.h>
  28. #ifdef USG5
  29. #include <fcntl.h>
  30. #endif
  31.  
  32. #ifndef O_RDONLY
  33. #define O_RDONLY 0
  34. #endif
  35.  
  36. #ifndef O_WRONLY
  37. #define O_WRONLY 1
  38. #endif
  39.  
  40. #include "lisp.h"
  41. #include "commands.h"
  42. #include "buffer.h"
  43. #include "paths.h"
  44.  
  45. #define max(a, b) ((a) > (b) ? (a) : (b))
  46.  
  47. Lisp_Object Vexec_path, Vexec_directory;
  48.  
  49. Lisp_Object Vshell_file_name;
  50.  
  51. #ifndef MAINTAIN_ENVIRONMENT
  52. /* List of strings to append to front of environment of
  53.    all subprocesses when they are started.  */
  54.  
  55. Lisp_Object Vprocess_environment;
  56. #endif
  57.  
  58. #ifdef BSD4_1
  59. /* Set nonzero when a synchronous subprocess is made,
  60.    and set to zero again when it is observed to die.
  61.    We wait for this to be zero in order to wait for termination.  */
  62. int synch_process_pid;
  63. #endif /* BSD4_1 */
  64.  
  65. /* True iff we are about to fork off a synchronous process or if we
  66.    are waiting for it.  */
  67. int synch_process_alive;
  68.  
  69. /* Nonzero => this is a string explaining death of synchronous subprocess.  */
  70. char *synch_process_death;
  71.  
  72. /* Exit code of synchronous subprocess if positive,
  73.    minus the signal number if negative.  */
  74. int synch_process_retcode;
  75.  
  76. Lisp_Object
  77. call_process_cleanup (fdpid)
  78.      Lisp_Object fdpid;
  79. {
  80.   register Lisp_Object fd, pid;
  81.   fd = Fcar (fdpid);
  82.   pid = Fcdr (fdpid);
  83.   close (XFASTINT (fd));
  84.   kill (XFASTINT (pid), SIGKILL);
  85.   return Qnil;
  86. }
  87.  
  88. #ifdef VMS
  89. #ifdef __GNUC__
  90. #define    environ $$PsectAttributes_NOSHR$$environ
  91. extern char **environ;
  92. #else
  93. extern noshare char **environ;
  94. #endif
  95. #else
  96. extern char **environ;
  97. #endif
  98.  
  99. DEFUN ("call-process", Fcall_process, Scall_process, 1, MANY, 0,
  100.   "Call PROGRAM in separate process.\n\
  101. Program's input comes from file INFILE (nil means /dev/null).\n\
  102. Insert output in BUFFER before point; t means current buffer;\n\
  103.  nil for BUFFER means discard it; 0 means discard and don't wait.\n\
  104. Fourth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
  105. Remaining arguments are strings passed as command arguments to PROGRAM.\n\
  106. Otherwise waits for PROGRAM to terminate\n\
  107. and returns a numeric exit status or a signal name as a string.\n\
  108. If you quit, the process is killed with SIGKILL.")
  109.   (nargs, args)
  110.      int nargs;
  111.      register Lisp_Object *args;
  112. {
  113.   Lisp_Object display, buffer, path;
  114.   int fd[2];
  115.   int filefd;
  116.   register int pid;
  117.   char buf[1024];
  118.   int count = specpdl_ptr - specpdl;
  119.   register unsigned char **new_argv
  120.     = (unsigned char **) alloca ((max (2, nargs - 2)) * sizeof (char *));
  121.   struct buffer *old = current_buffer;
  122.  
  123.   CHECK_STRING (args[0], 0);
  124.  
  125.   if (nargs <= 1 || NULL (args[1]))
  126.     args[1] = build_string (PATH_NULL);
  127.   else
  128.     args[1] = Fexpand_file_name (args[1], current_buffer->directory);
  129.  
  130.   CHECK_STRING (args[1], 1);
  131.  
  132.   {
  133.     register Lisp_Object tem;
  134.     buffer = tem = args[2];
  135.     if (nargs <= 2)
  136.       buffer = Qnil;
  137.     else if (!(EQ (tem, Qnil) || EQ (tem, Qt)
  138.            || XFASTINT (tem) == 0))
  139.       {
  140.     buffer = Fget_buffer (tem);
  141.     CHECK_BUFFER (buffer, 2);
  142.       }
  143.   }
  144.  
  145.   display = nargs >= 3 ? args[3] : Qnil;
  146.  
  147.   {
  148.     register int i;
  149.     for (i = 4; i < nargs; i++)
  150.       {
  151.     CHECK_STRING (args[i], i);
  152.     new_argv[i - 3] = XSTRING (args[i])->data;
  153.       }
  154.     /* Program name is first command arg */
  155.     new_argv[0] = XSTRING (args[0])->data;
  156.     new_argv[i - 3] = 0;
  157.   }
  158.  
  159.   filefd = open (XSTRING (args[1])->data, O_RDONLY, 0);
  160.   if (filefd < 0)
  161.     {
  162.       report_file_error ("Opening process input file", Fcons (args[1], Qnil));
  163.     }
  164.   /* Search for program; barf if not found.  */
  165.   openp (Vexec_path, args[0], "", &path, 1);
  166.   if (NULL (path))
  167.     {
  168.       close (filefd);
  169.       report_file_error ("Searching for program", Fcons (args[0], Qnil));
  170.     }
  171.   new_argv[0] = XSTRING (path)->data;
  172.  
  173.   if (XTYPE (buffer) == Lisp_Int)
  174. #ifdef VMS
  175.     fd[1] = open (PATH_NULL, 0), fd[0] = -1;
  176. #else
  177.     fd[1] = open (PATH_NULL, O_WRONLY), fd[0] = -1;
  178.   else
  179.     {
  180.       pipe (fd);
  181. #if 0
  182.       /* Replaced by close_process_descs */
  183.       set_exclusive_use (fd[0]);
  184. #endif
  185.     }
  186.  
  187.   synch_process_death = 0;
  188.   synch_process_retcode = 0;
  189.  
  190. #ifdef AMIGA
  191.   {
  192.     register unsigned char *temp;
  193.  
  194.     if (XTYPE (current_buffer->directory) == Lisp_String)
  195.       {
  196.     register int i;
  197.  
  198.     i = XSTRING (current_buffer->directory)->size;
  199.     temp = (unsigned char *) alloca (i + 1);
  200.     bcopy (XSTRING (current_buffer->directory)->data, temp, i);
  201.     temp[i] = 0;
  202.       }
  203.     pid = exec(new_argv[0], new_argv, filefd, fd[1], temp, amiga_process_stack_size);
  204.   }
  205. #else  
  206.   {
  207.     /* child_setup must clobber environ in systems with true vfork.
  208.        Protect it from permanent change.  */
  209.     register char **save_environ = environ;
  210.     register int fd1 = fd[1];
  211.     char **env;
  212.  
  213. #ifdef MAINTAIN_ENVIRONMENT
  214.     env = (char **) alloca (size_of_current_environ ());
  215.     get_current_environ (env);
  216. #else
  217.     env = environ;
  218. #endif /* MAINTAIN_ENVIRONMENT */
  219.  
  220.     pid = vfork ();
  221. #ifdef BSD4_1
  222.     /* cause SIGCHLD interrupts to look for this pid. */
  223.     synch_process_pid = pid;
  224. #endif /* BSD4_1 */
  225.  
  226.     if (pid == 0)
  227.       {
  228.     if (fd[0] >= 0)
  229.       close (fd[0]);
  230. #ifdef USG
  231. #ifdef HAVE_PTYS
  232.     setpgrp ();
  233. #endif
  234. #endif
  235.     child_setup (filefd, fd1, fd1, new_argv, env);
  236.       }
  237.  
  238.     environ = save_environ;
  239.  
  240.     close (filefd);
  241.     close (fd1);
  242.   }
  243. #endif /* not AMIGA */
  244.  
  245.   if (pid < 0)
  246.     {
  247.       close (fd[0]);
  248.       report_file_error ("Doing vfork", Qnil);
  249.     }
  250.  
  251.   if (XTYPE (buffer) == Lisp_Int)
  252.     {
  253. #ifndef subprocesses
  254.       wait_without_blocking ();
  255. #endif subprocesses
  256.       return Qnil;
  257.     }
  258.  
  259.   record_unwind_protect (call_process_cleanup,
  260.              Fcons (make_number (fd[0]), make_number (pid)));
  261.  
  262.  
  263.   if (XTYPE (buffer) == Lisp_Buffer)
  264.     Fset_buffer (buffer);
  265.  
  266.   immediate_quit = 1;
  267.   QUIT;
  268.  
  269.   {
  270.     register int nread;
  271.  
  272.     while ((nread = read (fd[0], buf, sizeof buf)) > 0)
  273.       {
  274.     immediate_quit = 0;
  275.     if (!NULL (buffer))
  276.       insert (buf, nread);
  277.     if (!NULL (display) && FROM_KBD)
  278.       redisplay_preserve_echo_area ();
  279.     immediate_quit = 1;
  280.     QUIT;
  281.       }
  282.   }
  283.  
  284.   /* Wait for it to terminate, unless it already has.  */
  285.   wait_for_termination (pid);
  286.  
  287.   immediate_quit = 0;
  288.  
  289.   set_buffer_internal (old);
  290.  
  291.   unbind_to (count);
  292.  
  293.   if (synch_process_death)
  294.     return build_string (synch_process_death);
  295.   return make_number (synch_process_retcode);
  296. }
  297.  
  298. DEFUN ("call-process-region", Fcall_process_region, Scall_process_region,
  299.   3, MANY, 0,
  300.   "Send text from START to END to a process running PROGRAM.\n\
  301. Delete the text if DELETE is non-nil.\n\
  302. Insert output in BUFFER before point; t means current buffer;\n\
  303.  nil for BUFFER means discard it; 0 means discard and don't wait.\n\
  304. Sixth arg DISPLAY non-nil means redisplay buffer as output is inserted.\n\
  305. Remaining args are passed to PROGRAM at startup as command args.\n\
  306. This function normally waits for the process to terminate;\n\
  307. if you quit, the process is killed.")
  308.   (nargs, args)
  309.      int nargs;
  310.      register Lisp_Object *args;
  311. {
  312.   register Lisp_Object filename_string, start, end;
  313.   char tempfile[20];
  314.  
  315.   strcpy (tempfile, PATH_TEMP);
  316.   mktemp (tempfile);
  317.  
  318.   filename_string = build_string (tempfile);
  319.   start = args[0];
  320.   end = args[1];
  321.   Fwrite_region (start, end, filename_string, Qnil, Qlambda);
  322.  
  323.   if (!NULL (args[3]))
  324.     Fdelete_region (start, end);
  325.  
  326.   args[3] = filename_string;
  327.   Fcall_process (nargs - 2, args + 2);
  328.   unlink (tempfile);
  329.   return Qnil;
  330. }
  331. #ifndef AMIGA
  332.  
  333. /* This is the last thing run in a newly forked inferior
  334.    either synchronous or asynchronous.
  335.    Copy descriptors IN, OUT and ERR as descriptors 0, 1 and 2.
  336.    Initialize inferior's priority, pgrp, connected dir and environment.
  337.    then exec another program based on new_argv.
  338.  
  339.    This function may change environ for the superior process.
  340.    Therefore, the superior process must save and restore the value
  341.    of environ around the vfork and the call to this function.
  342.  
  343.    ENV is the environment */
  344.  
  345. child_setup (in, out, err, new_argv, env)
  346.      int in, out, err;
  347.      register char **new_argv;
  348.      char **env;
  349. {
  350.   register int pid = getpid();
  351.  
  352.   setpriority (PRIO_PROCESS, pid, 0);
  353.  
  354. #ifdef subprocesses
  355.   /* Close Emacs's descriptors that this process should not have.  */
  356.   close_process_descs ();
  357. #endif
  358.  
  359.   /* Note that use of alloca is always safe here.  It's obvious for systems
  360.      that do not have true vfork or that have true (stack) alloca.
  361.      If using vfork and C_ALLOCA it is safe because that changes
  362.      the superior's static variables as if the superior had done alloca
  363.      and will be cleaned up in the usual way.  */
  364.  
  365.   if (XTYPE (current_buffer->directory) == Lisp_String)
  366.     {
  367.       register unsigned char *temp;
  368.       register int i;
  369.  
  370.       i = XSTRING (current_buffer->directory)->size;
  371.       temp = (unsigned char *) alloca (i + 2);
  372.       bcopy (XSTRING (current_buffer->directory)->data, temp, i);
  373.       if (temp[i - 1] != '/') temp[i++] = '/';
  374.       temp[i] = 0;
  375.       chdir (temp);
  376.     }
  377.  
  378. #ifndef MAINTAIN_ENVIRONMENT
  379.   /* Set `env' to a vector of the strings in Vprocess_environment.  */
  380.   {
  381.     register Lisp_Object tem;
  382.     register char **new_env;
  383.     register int new_length;
  384.  
  385.     new_length = 0;
  386.     for (tem = Vprocess_environment;
  387.      (XTYPE (tem) == Lisp_Cons
  388.       && XTYPE (XCONS (tem)->car) == Lisp_String);
  389.      tem = XCONS (tem)->cdr)
  390.       new_length++;
  391.  
  392.     /* new_length + 1 to include terminating 0 */
  393.     env = new_env = (char **) alloca ((new_length + 1) * sizeof (char *));
  394.  
  395.     /* Copy the env strings into new_env.  */
  396.     for (tem = Vprocess_environment;
  397.      (XTYPE (tem) == Lisp_Cons
  398.       && XTYPE (XCONS (tem)->car) == Lisp_String);
  399.      tem = XCONS (tem)->cdr)
  400.       *new_env++ = (char *) XSTRING (XCONS (tem)->car)->data;
  401.     *new_env = 0;
  402.   }
  403. #endif /* Not MAINTAIN_ENVIRONMENT */
  404.  
  405.   close (0);
  406.   close (1);
  407.   close (2);
  408.  
  409.   dup2 (in, 0);
  410.   dup2 (out, 1);
  411.   dup2 (err, 2);
  412.   close (in);
  413.   close (out);
  414.   close (err);
  415.  
  416. #ifdef USG
  417. #ifndef HAVE_PTYS
  418.   setpgrp ();            /* No arguments but equivalent in this case */
  419. #endif
  420. #else
  421.   setpgrp (pid, pid);
  422. #endif /* USG */
  423.   setpgrp_of_tty (pid);
  424.  
  425. #ifdef vipc
  426.   something missing here;
  427. #endif vipc
  428.  
  429.   /* execvp does not accept an environment arg so the only way
  430.      to pass this environment is to set environ.  Our caller
  431.      is responsible for restoring the ambient value of environ.  */
  432.   environ = env;
  433.   execvp (new_argv[0], new_argv);
  434.  
  435.   write (1, "Couldn't exec the program ", 26);
  436.   write (1, new_argv[0], strlen (new_argv[0]));
  437.   _exit (1);
  438. }
  439. #endif /* not AMIGA */
  440.  
  441. init_callproc ()
  442. {
  443.   register char * sh;
  444.   extern char **environ;
  445.   register char **envp;
  446.   Lisp_Object execdir;
  447.  
  448.   /* Turn PATH_EXEC into a path.  Don't look at environment.  */
  449.   Vexec_path = decode_env_path (0, PATH_EXEC);
  450.   Vexec_directory = Ffile_name_as_directory (Fcar (Vexec_path));
  451.   Vexec_path = nconc2 (decode_env_path ("PATH", ""), Vexec_path);
  452. #ifdef AMIGA
  453.   {
  454.     char *amiga_path(), *apath = amiga_path();
  455.  
  456.     Vexec_path = nconc2 (decode_env_path (0, apath), Vexec_path);
  457.     Vexec_path = nconc2 (decode_env_path (0, "GNUEmacs:c"), Vexec_path);
  458.     free(apath);
  459.   }
  460. #endif  
  461.  
  462.   execdir = Fdirectory_file_name (Vexec_directory);
  463.   if (access (XSTRING (execdir)->data, 0) < 0)
  464.     {
  465.       printf ("Warning: executable/documentation dir (%s) does not exist.\n",
  466.           XSTRING (Vexec_directory)->data);
  467.       sleep (2);
  468.     }
  469.  
  470.   sh = (char *) egetenv ("SHELL");
  471.   Vshell_file_name = build_string (sh ? sh : PATH_SHELL);
  472.  
  473. #ifndef MAINTAIN_ENVIRONMENT
  474.   /* The equivalent of this operation was done
  475.      in init_environ in environ.c if MAINTAIN_ENVIRONMENT */
  476.   Vprocess_environment = Qnil;
  477. #ifndef CANNOT_DUMP
  478.   if (initialized)
  479. #endif
  480.     for (envp = environ; *envp; envp++)
  481.       Vprocess_environment = Fcons (build_string (*envp),
  482.                     Vprocess_environment);
  483. #endif /* MAINTAIN_ENVIRONMENT */
  484. }
  485.  
  486. syms_of_callproc ()
  487. {
  488.   DEFVAR_LISP ("shell-file-name", &Vshell_file_name,
  489.     "*File name to load inferior shells from.\n\
  490. Initialized from the SHELL environment variable.");
  491.  
  492.   DEFVAR_LISP ("exec-path", &Vexec_path,
  493.     "*List of directories to search programs to run in subprocesses.\n\
  494. Each element is a string (directory name) or nil (try default directory).");
  495.  
  496.   DEFVAR_LISP ("exec-directory", &Vexec_directory,
  497.     "Directory that holds programs that come with GNU Emacs,\n\
  498. intended for Emacs to invoke.");
  499.  
  500. #ifndef MAINTAIN_ENVIRONMENT
  501.   DEFVAR_LISP ("process-environment", &Vprocess_environment,
  502.     "List of strings to append to environment of subprocesses that are started.\n\
  503. Each string should have the format ENVVARNAME=VALUE.");
  504. #endif
  505.  
  506.   defsubr (&Scall_process);
  507.   defsubr (&Scall_process_region);
  508. }
  509.